package com.anytrek.cloudcommodity.controller;

import com.anytrek.cloudcommodity.service.CommodityService;
import com.anytrek.cloudcommon.utils.ApiResult;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ ClassName CommodityController
 * @ Author Alex2
 * @ Date 2025/8/29 15:22
 **/
@RestController
@RequestMapping("commodity")
@RequiredArgsConstructor
public class CommodityController {

    private final CommodityService commodityService;

    // http://localhost:8888/commodity/insertCommodity?name=Knife&price=5&stock=10
    @GetMapping("insertCommodity")
    public ApiResult insertCommodity(@RequestParam("name") String name,
                                     @RequestParam("price") Integer price,
                                     @RequestParam("stock") Integer stock) {
        return commodityService.insertCommodity(name, price, stock);
    }

    @GetMapping("getPriceByCommodityId")
    public ApiResult getPriceByCommodityId(@RequestParam("commodityId") Integer commodityId) {
        return commodityService.getPriceByCommodityId(commodityId);
    }

    @GetMapping("deductStock")
    public ApiResult deductStock(@RequestParam("commodityId") Integer commodityId,
                                 @RequestParam("count") Integer count) {
        return commodityService.deductStock(commodityId, count);
    }
}
